package dao;

import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import modelo.Freelancer;
import modelo.Lance;
import modelo.Projeto;


public class LanceDAO implements Serializable {
    private final EntityManager manager;
    private final DAO<Lance,Long> dao;
    
    public LanceDAO(EntityManager manager) {
        this.manager = manager;
        dao = new DAO(manager,Lance.class);
    }
    
    public List<Lance> listar(){
        return dao.listar();
    }
    
    public Lance consultar(Long id) {
        return dao.consultar(id);
    }
    
    public void inserir(Lance lance) {
        
        this.dao.inserir(lance);
    
        
    }
    
    public boolean existeLance(Lance lance) {
    	 String sql = "select count(lance) from Lance lance " +
    	 		"where (lance.freelancer = :pfreelancer and lance.projeto = :pprojeto) ";
    	 
    	 Query consulta = manager.createQuery(sql);
    	 
    	 consulta.setParameter("pfreelancer", lance.getFreelancer());
    	 consulta.setParameter("pprojeto",lance.getProjeto());
    	 
    	 Long nrLances = (Long) consulta.getSingleResult();
    	 
    	 return (nrLances == 1);
    	 
    	
    }
    
  
}
    
